package ${PACKAGE_NAME};

import java.util.List;


import ${PARENT_PACKAGE_NAME}.inter.${NAME}Mapper;
import ${PARENT_PACKAGE_NAME}.model.${NAME};
import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.DateUtil;
import com.eastdigit.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ${NAME}Service extends BaseService {

    @Autowired
    private ${NAME}Mapper ${LOWER_NAME}Mapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject find${NAME}(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("", new Object[0]);
        return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 新增
     *
     * @param reqBean
     */
    public void add${NAME}(ReqBean reqBean) {
        ${NAME} ${LOWER_NAME} = get${NAME}(reqBean);
        <#list colList as column>
        <#if (column.classColName=="Id")>
        ${LOWER_NAME}.setId(intId());// 手动设置Id
        <#elseif (column.classColName=="MakeTime")>
        ${LOWER_NAME}.setMakeTime(${LOWER_NAME}.getModifyTime());
        </#if>
        </#list>

        ${LOWER_NAME}Mapper.insert(${LOWER_NAME});
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void update${NAME}(ReqBean reqBean) {
        <#list pkList as column>
        String ${column.formColName} = reqBean.getInfo("${column.formColName}");
        </#list>
        if (<#list pkList as column>StringUtil.isEmpty(${column.formColName})<#if (column_has_next)> || </#if></#list>) {
            throw new BusinessException("获取主键失败");
        }
        ${NAME} ${LOWER_NAME} = get${NAME}(reqBean);
        ${LOWER_NAME}Mapper.updateByPrimaryKeySelective(${LOWER_NAME});
    }

    /**
     * 删除
     *
     * @param reqBean
     */
    public void remove${NAME}(ReqBean reqBean) {
        <#list pkList as column>
        String ${column.formColName} = reqBean.getInfo("${column.formColName}");
        </#list>
        if (<#list pkList as column>StringUtil.isEmpty(${column.formColName})<#if (column_has_next)> || </#if></#list>) {
            throw new BusinessException("获取主键失败");
        }
        ${LOWER_NAME}Mapper.deleteByPrimaryKey(<#list pkList as column><#if (column.dataType==4)>Integer.parseInt(${column.formColName})<#else>${column.formColName}</#if><#if (column_has_next)>, </#if></#list>);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private ${NAME} get${NAME}(ReqBean reqBean) {
        ${NAME} ${LOWER_NAME} = new ${NAME}();

        <#list colList as column>
        <#if (column.classColName=="Operator")>
        ${LOWER_NAME}.setOperator(reqBean.getUserId());
        <#elseif (column.classColName=="ModifyTime")>
        ${LOWER_NAME}.setModifyTime(new Date());
        <#elseif (column.dataType==4)>
        ${LOWER_NAME}.set${column.classColName}(reqBean.getInfoInt("${column.formColName}"));
        <#elseif (column.dataType==8)>
        ${LOWER_NAME}.set${column.classColName}(reqBean.getInfoDouble("${column.formColName}"));
        <#elseif (column.dataType==91 || column.dataType==93)>
        ${LOWER_NAME}.set${column.classColName}(DateUtil.parseDateTime(reqBean.getInfo("${column.formColName}")));
        <#elseif (column.classColName!="MakeTime")>
        ${LOWER_NAME}.set${column.classColName}(reqBean.getInfo("${column.formColName}"));
        </#if>
        </#list>

        return ${LOWER_NAME};
    }

}